package org.duang.logs;

import java.io.File;
import java.nio.charset.Charset;

import org.duang.config.Config;
import org.duang.kit.PathKit;
import org.duang.logs.db.ToolUtils;
//import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;

public class LogBackLogger extends org.duang.logs.Logger {

	private Logger logger;
	private static int step = 0;

	LogBackLogger(Class<?> clazz) {
		logger = (Logger) LoggerFactory.getLogger(clazz);
		// LoggerContext loggerContext = logger.getLoggerContext();
		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		// 综合时间与大小的滚动策略，先按小时滚动，小时内的文件大于10mb时再按大小滚动
		// FileAppender<ILoggingEvent> fileAppender = new
		// FileAppender<ILoggingEvent>();
		RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
		TimeBasedRollingPolicy<ILoggingEvent> timePolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
		timePolicy.setFileNamePattern(getFileNamePattern());
		SizeAndTimeBasedFNATP<ILoggingEvent> timeBasedTriggering = new SizeAndTimeBasedFNATP<ILoggingEvent>();
		timeBasedTriggering.setMaxFileSize("10MB");
		// timeBasedTriggering.start();
		timePolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggering);
		timePolicy.setMaxHistory(30); // 保留 30天数据
		timePolicy.setContext(loggerContext);
		timePolicy.setParent(fileAppender);
		timePolicy.start(); // 必须启动

		// 设置输出样式
		PatternLayoutEncoder encoder = new PatternLayoutEncoder();
		encoder.setContext(loggerContext);
		encoder.setPattern("%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
		encoder.setCharset(Charset.forName("UTF-8"));
		encoder.start(); // 必须启动

		fileAppender.setFile(getLogFilePath());
		fileAppender.setEncoder(encoder);
		fileAppender.setRollingPolicy(timePolicy);
		fileAppender.setContext(loggerContext);
		fileAppender.start(); // 必须启动

		logger.addAppender(fileAppender);
		logger.setLevel(ToolUtils.getLevel());
		logger.setAdditive(ToolUtils.isAdditive()); // 不继承Root的appender,
													// 即是否在控制台输出,true为输出

	}

	private String getBaseLogFilePath() {
		return PathKit.getWebRootPath() + File.separator + "logs";
	}

	private String getLogFilePath() {
		String path = getBaseLogFilePath() + File.separator + Config.getProductCode() + ".log";
		if((step++) == 0) {
			System.out.println("########duang log file path: " + path);
		}
		return path;
		// return getBaseLogFilePath()+File.separator+"test.log";
	}

	private String getFileNamePattern() {
		return getBaseLogFilePath() + File.separator + Config.getProductCode() + ".%d{yyyy-MM-dd_HH}.%i.zip";
		// return
		// getBaseLogFilePath()+File.separator+"test.%d{yyyy-MM-dd_HH}.%i.zip";
	}

	@Override
	public void print(String msg) {
		if (logger.isDebugEnabled()) {
			logger.debug(msg);
		} else if (logger.isInfoEnabled()) {
			logger.info(msg);
		} else if (logger.isWarnEnabled()) {
			logger.warn(msg);
		} else if (logger.isErrorEnabled()) {
			logger.error(msg);
		}
	}

	@Override
	public void print(String msg, Throwable t) {
		if (logger.isDebugEnabled()) {
			logger.debug(msg, t);
		} else if (logger.isInfoEnabled()) {
			logger.info(msg, t);
		} else if (logger.isWarnEnabled()) {
			logger.warn(msg, t);
		} else if (logger.isErrorEnabled()) {
			logger.error(msg, t);
		}
	}

	@Override
	public boolean isDebugEnabled() {
		return logger.isDebugEnabled();
	}

	@Override
	public boolean isInfoEnabled() {
		return logger.isInfoEnabled();
	}

	@Override
	public boolean isWarnEnabled() {
		return logger.isWarnEnabled();
	}

	@Override
	public boolean isErrorEnabled() {
		return logger.isErrorEnabled();
	}
}
